from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
import uuid
from qiniu import Auth,put_file,etag
from appqiniu.models import *
from django.core.mail import send_mail
from djang_ol import settings
from django.http import JsonResponse
# Create your views here.

# class PicToQiniu(APIView):
#   def post(self,request):
#     data = request.data
#     pic = data["pic"]
#
#     uuid_str = str(uuid.uuid4())
#     pic_name =uuid_str + pic.name
#     try:
#       with open(pic_name,"wb") as f:
#         for i in pic.chunks():
#           f.write(i)
#       AK = "LYyYQzkYNdzswDd84W0m5z5YzBMBDVaOziK8EbKO"
#       SK = "2Cejvu_Ab3MRnZrm3ZAsJZK46EAz-5yxKWQL5lh9"
#       q = Auth(AK,SK)
#       bucket_name = 'djangool'
#       key = pic_name
#       token = q.upload_token(bucket_name,key,3600)
#       localfile = './%s' % pic_name
#       ret,info = put_file(token,key,localfile)
#
#       url = "http://q5tsjovio.bkt.clouddn.com" + pic_name
#       Use.objects.create(name=data["name"],age=data["age"],
#                          phone=data["phone"],email=data["email"])
#       res_data = {
#         "code":200,
#         "msg":"成功",
#         "url":url
#       }
#       return Response(res_data)
#     except Exception as e:
#       res_data = {
#         "code":400,
#         "msg":"失败"
#       }
#       return Response(res_data)

def send_email_info(request):
  subject="主题"  # 主题
  message="this is test email"  # 内容
  sender=settings.EMAIL_FROM  # 发送邮箱，已经在settings.py设置，直接导入
  receiver=["1244104686@qq.com"]
  # print(receiver)
  html_message='<h1>%s</h1> 惦记我跳转到百度：www.baidu.com' % "测试我的信息"
  send_mail(subject, message, sender, receiver, html_message=html_message)
  return JsonResponse({"ok":"ok"})



class Meail_infom(APIView):
  def post(self,request):
    subject = "主题"  # 主题
    message = "this is test email"  # 内容
    sender = settings.EMAIL_FROM  # 发送邮箱，已经在settings.py设置，直接导入
    receiver = [request.data["email"]]
    print(receiver)
    html_message = '<h1>%s</h1> 惦记我跳转到百度：www.baidu.com' % "测试我的信息"
    send_mail(subject, message, sender, receiver, html_message=html_message)
    return JsonResponse(status=200,data={
      "code":200,
      "msg":"发送成功"
    })
